package com.liusand.client.controller;


import com.liusand.common.thread.UsersThreadLocal;
import com.liusand.model.dto.PatientDto;
import com.liusand.model.pojo.Patient;
import com.liusand.model.vo.PatientVo;
import com.liusand.model.vo.Result;
import com.liusand.service.IPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 就诊人 前端控制器
 * </p>
 *
 * @author liusand
 * @since 2023-01-29
 */
@RestController
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private IPatientService patientService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/list")
    public Result list(){
        List<PatientVo> list = patientService.listById();
        return Result.success(list);
    }

    @GetMapping
    public Result getPatient(@RequestParam String patientId){
        Patient patient = patientService.getPatient(patientId);
        return Result.success(patient);
    }

    @PostMapping("/save")
    public Result savePatient(String patientId, @RequestBody PatientDto patientDto){
        Boolean result = patientService.savePatient(patientId, patientDto);
        if (!result){
            return Result.error(500, "保存失败");
        }
        redisTemplate.delete("HEALTH_CACHE:PATIENT_ID_" + UsersThreadLocal.getId());
        return Result.success(null);
    }

    @PostMapping("/remove")
    public Result removePatient(@RequestParam String patientId){
        boolean b = patientService.removeById(patientId);
        if(!b){
            return Result.error(500, "删除失败");
        }
        redisTemplate.delete("HEALTH_CACHE:PATIENT_ID_" + UsersThreadLocal.getId());
        return Result.success(null);
    }
}

